﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NF_Tools
{
    public partial class BF_PopMenu : System.Windows.Forms.ContextMenuStrip
    {
        public BF_PopMenu(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            this.Font = new System.Drawing.Font("微软雅黑", 11F);
            this.Size = new System.Drawing.Size(61, 4);
            this.Renderer = new ProfessionalToolStripRendererEx();
                
        }

        #region 自定义属性

        private string[] subItem;

        [Browsable(true), Category("pan"), Description("模拟菜单文字集合")]
        public string[] SubItem
        {
            get
            {
                return subItem;
            }
            set
            {
                subItem = value;
            }
        }

        private bool _Checkit;
        public bool Checkit
        {
            get { return _Checkit; }
            set
            {
                if (value)
                    CreatItem();
                _Checkit = value;
            }
        }

        #endregion

        #region 自定义事件
        public delegate void UserClickDelegate(object sender, PopEventArgs e);
        [Browsable(true), Description("菜单点击事件"), Category("")]
        public event UserClickDelegate UserClickEvent;

        void tslItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem tItem=(ToolStripMenuItem)sender;
            int index = this.Items.IndexOf(tItem);
            
            if (UserClickEvent != null)
                UserClickEvent(sender, new PopEventArgs(index));
        }
        #endregion

        #region 方法
        /// <summary>
        /// 添加集合到列表
        /// </summary>
        public void CreatItem()
        {
            ToolStripMenuItem tslItem = null;
            //ToolStripMenuItem tslWay = null;
            this.Items.Clear();
            if (subItem == null) return;
            //添加集合
            for (int i = 0; i < subItem.Length; i++)
            {
                tslItem = new ToolStripMenuItem();
                tslItem.Text = subItem[i];
                tslItem.AutoSize = false;
                tslItem.Height = 40;
                tslItem.Width = 200;
                tslItem.TextAlign = ContentAlignment.MiddleCenter;
                tslItem.Click += tslItem_Click;
                this.Items.Add(tslItem);

                //if (kv.Value == null) continue;
                //for (int i = 0; i < kv.Value.Length; i++)
                //{
                //    tslWay = new ToolStripMenuItem();
                //    tslWay.Text = kv.Value[i];
                //    tslWay.AutoSize = false;
                //    tslWay.Height = 40;
                //    tslWay.Width = 200;
                //    tslWay.TextAlign = ContentAlignment.MiddleCenter;
                //    tslWay.Click += tslItem_Click;
                //    tslItem.DropDownItems.Add(tslWay);
                //}
            }
        }
        #endregion
    }
    public class PopEventArgs : EventArgs 
    {
        public int Index { get; set; }
        public PopEventArgs(int  index)
        {
            Index = index;
        }
       
    }
    public class ProfessionalToolStripRendererEx : ToolStripProfessionalRenderer
    {
        ColorConfig colorconfig = new ColorConfig();//创建颜色配置类  
        /// <summary>  
        /// 渲染整个背景  
        /// </summary>  
        /// <param name="e"></param>  
        protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
        {
            e.ToolStrip.ForeColor = colorconfig.FontColor;
            //如果是下拉  
            if (e.ToolStrip is ToolStripDropDown)
            {
                e.Graphics.FillRectangle(new SolidBrush(colorconfig.DropDownItemBackColor), e.AffectedBounds);
            }
            else
            {
                base.OnRenderToolStripBackground(e);
            }
        }
        /// <summary>  
        /// 渲染下拉左侧图标区域  
        /// </summary>  
        /// <param name="e"></param>  
        protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
        {
            FillLineGradient(e.Graphics, e.AffectedBounds, colorconfig.MarginStartColor, colorconfig.MarginEndColor, 0f, null);
        }
        /// <summary>  
        /// 渲染菜单项的背景  
        /// </summary>  
        /// <param name="e"></param>  
        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
           if (e.ToolStrip is ToolStripDropDown)
            {
                if (e.Item.Selected)
                {
                    FillLineGradient(e.Graphics, new Rectangle(0, 0, e.Item.Size.Width, e.Item.Size.Height), colorconfig.DropDownItemStartColor, colorconfig.DropDownItemEndColor, 90f, null);
                    e.Item.ForeColor = Color.FromArgb(233, 81, 29);
                }
                else
                {
                    e.Item.ForeColor = colorconfig.FontColor;
                }
            }
            else
            {
                base.OnRenderMenuItemBackground(e);
            }
        }
        /// <summary>  
        /// 渲染菜单项的分隔线  
        /// </summary>  
        /// <param name="e"></param>  
        protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
        {
            e.Graphics.DrawLine(new Pen(colorconfig.SeparatorColor), 0, 2, e.Item.Width, 2);
        }
        /// <summary>  
        /// 渲染边框  
        /// </summary>  
        /// <param name="e"></param>  
        protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
        {
            if (e.ToolStrip is ToolStripDropDown)
            {
                e.Graphics.DrawRectangle(new Pen(colorconfig.DropDownBorder), new Rectangle(0, 0, e.AffectedBounds.Width - 1, e.AffectedBounds.Height - 1));
            }
            else
            {
                base.OnRenderToolStripBorder(e);
            }
        }

        /// <summary>
        /// 渲染文字
        /// </summary>
        /// <param name="e"></param>
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            //主要让文字居中
            e.Graphics.DrawString(e.Text, e.TextFont, new SolidBrush(e.TextColor), new PointF(25,(e.Item.Height- e.Item.Font.Height)/2));
            e.Text = "";
            base.OnRenderItemText(e);
        }

        /// <summary>  
        /// 渲染箭头  
        /// </summary>  
        /// <param name="e"></param>  
        protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
        {
            e.ArrowColor = Color.LightBlue;
            base.OnRenderArrow(e);
        }

        /// <summary>  
        /// 填充线性渐变  
        /// </summary>  
        /// <param name="g">画布</param>  
        /// <param name="rect">填充区域</param>  
        /// <param name="startcolor">开始颜色</param>  
        /// <param name="endcolor">结束颜色</param>  
        /// <param name="angle">角度</param>  
        /// <param name="blend">对象的混合图案</param>  
        private void FillLineGradient(Graphics g, Rectangle rect, Color startcolor, Color endcolor, float angle, Blend blend)
        {
            LinearGradientBrush linebrush = new LinearGradientBrush(rect, startcolor, endcolor, angle);
            if (blend != null)
            {
                linebrush.Blend = blend;
            }
            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(rect);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.FillPath(linebrush, path);
        }
    }
    public class ColorConfig
    {
        private Color _fontcolor = Color.FromArgb(90, 90, 90);
        /// <summary>  
        /// 菜单字体颜色  
        /// </summary>  
        public Color FontColor
        {
            get { return this._fontcolor; }
            set { this._fontcolor = value; }
        }
        private Color _marginstartcolor = Color.White; 
        /// <summary>  
        /// 下拉菜单坐标图标区域开始颜色  
        /// </summary>  
        public Color MarginStartColor
        {
            get { return this._marginstartcolor; }
            set { this._marginstartcolor = value; }
        }
        private Color _marginendcolor = Color.White; 
        /// <summary>  
        /// 下拉菜单坐标图标区域结束颜色  
        /// </summary>  
        public Color MarginEndColor
        {
            get { return this._marginendcolor; }
            set { this._marginendcolor = value; }
        }
        private Color _dropdownitembackcolor = Color.White;
        /// <summary>  
        /// 下拉项背景颜色  
        /// </summary>  
        public Color DropDownItemBackColor
        {
            get { return this._dropdownitembackcolor; }
            set { this._dropdownitembackcolor = value; }
        }
        private Color _dropdownitemstartcolor = Color.White; 
        /// <summary>  
        /// 下拉项选中时开始颜色  
        /// </summary>  
        public Color DropDownItemStartColor
        {
            get { return this._dropdownitemstartcolor; }
            set { this._dropdownitemstartcolor = value; }
        }
        private Color _dorpdownitemendcolor = Color.WhiteSmoke; 
        /// <summary>  
        /// 下拉项选中时结束颜色  
        /// </summary>  
        public Color DropDownItemEndColor
        {
            get { return this._dorpdownitemendcolor; }
            set { this._dorpdownitemendcolor = value; }
        }
    
        private Color _separatorcolor = Color.Gray;
        /// <summary>  
        /// 分割线颜色  
        /// </summary>  
        public Color SeparatorColor
        {
            get { return this._separatorcolor; }
            set { this._separatorcolor = value; }
        }
      
        private Color _dropdownborder = Color.FromArgb(228, 228, 228);
        /// <summary>  
        /// 下拉区域边框颜色  
        /// </summary>  
        public Color DropDownBorder
        {
            get { return this._dropdownborder; }
            set { this._dropdownborder = value; }
        }
    }  
}
   